package C036;

import java.util.*;

/**
 * ClassName: C036
 * Package: C036
 * Description:
 *
 * @Author BCXJ
 * @Create 2025/1/19 22:51
 * @Version 1.0
 * @Since 1.0
 */
public class C01 {
    public static void main(String[] args) {
        TreeNode treeNode1 = new TreeNode(1);
        TreeNode treeNode2 = new TreeNode(2);
        TreeNode treeNode3 = new TreeNode(3);
        TreeNode treeNode4 = new TreeNode(4);
        treeNode1.left = treeNode2;
        treeNode1.right = treeNode3;
        treeNode3.left = treeNode4;
        System.out.println(levelOrder1(treeNode1));
        System.out.println(levelOrder2(treeNode1));
        System.out.println(levelOrder3(treeNode1));
    }

    // 不提交这个类
    public static class TreeNode {
        public int val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }
    }

    // 递归实现
    public static List<List<Integer>> levelOrder3(TreeNode root) {
        List<List<Integer>> ans = new ArrayList<>();
        process(root, ans, 0);
        return ans;
    }

    private static void process(TreeNode root, List<List<Integer>> ans, int level) {
        if(root == null) {
            return;
        }
        if(ans.size() == level) {
            ans.add(new ArrayList<>());
        }
        ans.get(level).add(root.val);
        process(root.left, ans, level + 1);
        process(root.right, ans, level + 1);
    }

    // 提交时把方法名改为levelOrder，此方法为普通bfs，此题不推荐
    public static List<List<Integer>> levelOrder1(TreeNode root) {
        // ans队列
        ArrayList<List<Integer>> ans = new ArrayList<>();

        // 队列
        Queue<TreeNode> queue = new LinkedList<>();
        HashMap<TreeNode, Integer> map = new HashMap<>(); // key 为 val，value为level(层数)

        queue.add(root);
        map.put(root, 0);
        while(! queue.isEmpty()) {
            TreeNode node = queue.poll();
            Integer level = map.get(node);
            if(ans.size() == level) {
                ArrayList<Integer> childAns = new ArrayList<>();
                ans.add(childAns);
            }
            ans.get(level).add(node.val);
            if(node.left != null) {
                queue.add(node.left);
                map.put(node.left, level + 1);
            }
            if(node.right != null) {
                queue.add(node.right);
                map.put(node.right, level + 1);
            }
        }
        return ans;
    }


    // 数组队列解决

    public static int intMAXLEN= 2001;
    public static TreeNode[] queue = new TreeNode[intMAXLEN];
    public static int r,l;
    public static List<List<Integer>> levelOrder2(TreeNode root) {
        List<List<Integer>> ans = new ArrayList<>();
        if(root == null) {
            return ans;
        }

        queue[0] = root;
        l = 0;
        r = 1;
        int n = 0;
        while(l < r) {
            n = r - l;
            ArrayList<Integer> childAns = new ArrayList<>();
            for (int i = 0; i < n; i++) {
                TreeNode cur = queue[l++];
                if(cur.left != null) {
                    queue[r] = cur.left;
                    r ++;
                }
                if(cur.right != null) {
                    queue[r] = cur.right;
                    r ++;
                }
                childAns.add(cur.val);
            }
            ans.add(childAns);
        }

        return ans;
    }
}
